﻿using System;
using System.Collections;
using System.Drawing;
using System.Text;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Model;

public partial class NumeCopii : System.Web.UI.Page
{
    protected Categories current;
    protected void Page_Load(object sender, EventArgs e)
    {
        HtmlGenericControl topLiItem = (HtmlGenericControl)Master.FindControl("liBabyNames");
        topLiItem.Attributes.Add("class", "current");

        current = Entities.GetCategoryDetails(11);
        litNav.Text = LoadNavigation(current);
        litTitle.Text = current.Title;
        litContent.Text = current.Description;
        if (!string.IsNullOrEmpty(current.ImagePath))
        {
            Bitmap img = new Bitmap(Utils.GetApplicationSetting(Constants.IMAGES_CATEGORIES_PHYSICAL_PATH) + current.ImagePath);
            if (img.Width > img.Height) imgCateg.Width = new Unit(250);
            else imgCateg.Height = new Unit(250);
            imgCateg.ImageUrl = Utils.GetApplicationSetting(Constants.IMAGES_CATEGORIES_URL_PREFIX) + current.ImagePath;
        }
        else
            imgCateg.Visible = false;
            //imgCateg.ImageUrl = Utils.GetApplicationSetting(Constants.IMAGES_CATEGORIES_URL_PREFIX) + Constants.NO_IMAGE_FILE_NAME;
        
        imgCateg.AlternateText = current.Title;

        dlOrigin.DataSource = Entities.GetNamesOrigins();
        dlOrigin.DataBind();
        dlOrigin.Items.Insert(0, new ListItem("Toate", ""));

        Title = "Nume copii";
    }
    protected string LoadNavigation(Categories categ)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append(@"<a href=""default.aspx"">Prima pagina</a>");

        Categories current = categ;
        ArrayList nav = new ArrayList();
        nav.Add(current);
        while (current.Parent.CategoryID != 0)
        {
            try
            {
                current = current.Parent;
                nav.Add(current);
            }
            catch { break; }
        }
        for (int i = nav.Count - 1; i >= 0; i--)
        {
            Categories x = (Categories)nav[i];
            if (categ.CategoryID != Convert.ToInt32(x.CategoryID))
                sb.Append(@" » <a href=""category.aspx?ID=" + x.CategoryID + @""">" + x.Title + @"</a>");
            else
                sb.Append(@" » " + x.Title);
        }
        return sb.ToString();
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        DataGridBinder();
    }
    protected void dgNames_PageChanger(object source, DataGridPageChangedEventArgs e)
    {
        dgNames.CurrentPageIndex = e.NewPageIndex;
        DataGridBinder();
    }

    protected void DataGridBinder()
    {
        dgNames.DataSource = Entities.SearchNames(dlSex.SelectedIndex, dlOrigin.SelectedValue, txtPrefix.Text.Trim());
        dgNames.Style.Add("border-collapse", "separate");
        dgNames.DataBind();
    }
}
